package com.application.fabric.test.ca_become;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.PrivateKey;
import java.util.Set;

import org.hyperledger.fabric.gateway.*;
import org.hyperledger.fabric.sdk.Enrollment;
import org.hyperledger.fabric.sdk.User;
public class 数字证书的使用 {
    public static void main(String[] args) throws Exception {
        // 加载用户数字证书和私钥文件
        byte[] certBytes = Files.readAllBytes(Paths.get("user.crt"));
        byte[] keyBytes = Files.readAllBytes(Paths.get("user.key"));

        // 创建一个实现Enrollment接口的类来表示用户的身份
        Enrollment enrollment = new Enrollment() {
            @Override
            public PrivateKey getKey() {
                // 从私钥字节数组中加载私钥
                return loadPrivateKey(keyBytes);
            }

            @Override
            public String getCert() {
                // 将数字证书字节数组转换为字符串形式
                return new String(certBytes);
            }
        };

        // 创建一个实现User接口的类来表示Fabric网络中的用户
        User user = new User() {
            @Override
            public String getName() {
                return "user";
            }

            @Override
            public Set<String> getRoles() {
                return null;
            }

            @Override
            public String getMspId() {
                return "Org1MSP";
            }

            @Override
            public Enrollment getEnrollment() {
                return enrollment;
            }

            @Override
            public String getAccount() {
                return null;
            }

            @Override
            public String getAffiliation() {
                return null;
            }
        };

        // 使用Gateway连接到Fabric网络并验证用户身份信息
        Gateway.Builder builder = Gateway.createBuilder();
//        builder.identity(user);
        builder.networkConfig(Paths.get("connection.json"));
        try (Gateway gateway = builder.connect()) {
            System.out.println("User authenticated successfully.");
        }

    }

    private static PrivateKey loadPrivateKey(byte[] keyBytes) {
        return null;
    }
}
